# -*- coding:utf-8 -*-

from feature_data.Dict2Coauthor_Graphfeature import Feature_coauthor_extration
from nd_service.first_stage_graph import Graph_coauthor_stage

from feature_data.Dict2Org_Graphfeature import Feature_org_extration
from nd_service.second_stage_graph import Graph_org_stage
from answer_extration import Answer_extration
from nd_utils.networkx_util import *
from co_org_venue_cluster import Co_org_venue_Cluster

class Feature_org_venue_extration():
    def __init__(self, xml2dict):
        self.xml2dict = xml2dict

        self.publication_dict = self.xml2dict.publication_dict
        self.paperid_idx_dict = self.xml2dict.paperid_idx_dict

        self.paperidx_org_dict = self._get_paperidx_org_dict()
        self.paperidx_venue_dict = self._get_paperidx_venue_dict()

        self.first_stage_clust = self._get_first_stage_clust()
        self.init_graph = self._get_cluster_graph()

        self.first_subgraph_nodes_list = self.first_stage_clust.first_subgraph_nodes_list

        self.co_org_venue_Cluster_list = self._get_co_org_venue_Cluster_list()
        self.cluster_co_org_venue_pair_set_list = self._get_cluster_co_org_venue_pair_set_list()

    def _get_paperidx_org_dict(self):
        paper_org_dict = {}
        for paperid, publication_attr_dict in self.publication_dict.items():
            organization =  publication_attr_dict['organization']
            paper_org_dict[ self.paperid_idx_dict[paperid] ] = organization
        return paper_org_dict

    """
        idx : title
    """
    def _get_paperidx_venue_dict(self):
        paper_venue_dict = {}
        for paperid, publication_attr_dict in self.publication_dict.items():
            venue =  publication_attr_dict['jconf']
            paper_venue_dict[ self.paperid_idx_dict[paperid] ] = venue
        return paper_venue_dict

    def _get_first_stage_clust(self):
        feature_coauthor_extration = Feature_coauthor_extration(self.xml2dict)
        answer_extration = Answer_extration(self.xml2dict)
        first_stage_clust = Graph_coauthor_stage(feature_coauthor_extration, answer_extration)
        return first_stage_clust


    def _get_cluster_graph(self):
        flag = self.first_stage_clust.simulation_occur_flag
        if flag:
            return self.first_stage_clust.first_cluster_graph
        else:
            return self.first_stage_clust.init_graph

    def _get_co_org_venue_Cluster_list(self):
        co_org_venue_Cluster_list = []
        for paperidx_list in self.first_subgraph_nodes_list:
            coauthorCluster = Co_org_venue_Cluster( self.paperidx_org_dict, self.paperidx_venue_dict, paperidx_list)
            co_org_venue_Cluster_list.append( coauthorCluster )
        return co_org_venue_Cluster_list

    def _get_cluster_co_org_venue_pair_set_list(self):
        cluster_co_org_venue_pair_set_list = []
        for i in range( len(self.co_org_venue_Cluster_list) -1 ):
            for j in range(i+1, len(self.co_org_venue_Cluster_list) ):
                org_set1 = self.co_org_venue_Cluster_list[i].cluster_org_set
                venue_set1 = self.co_org_venue_Cluster_list[i].cluster_venue_set

                org_set2 = self.co_org_venue_Cluster_list[j].cluster_org_set
                venue_set2 = self.co_org_venue_Cluster_list[j].cluster_venue_set

                coorg_set =  org_set1.intersection(org_set2)
                covenue_set =  venue_set1.intersection(venue_set2)

                if len(coorg_set) != 0 and len(covenue_set) != 0:
                    # 取 交集单位中的第一个
                    org = list(coorg_set)[0]
                    idx1 = self.co_org_venue_Cluster_list[i].get_cluster_idx_by_org(org)
                    idx2 = self.co_org_venue_Cluster_list[j].get_cluster_idx_by_org(org)
                    cluster_co_org_venue_pair_set_list.append( set([idx1, idx2]) )

                    venue = list(covenue_set)[0]
                    idx1 = self.co_org_venue_Cluster_list[i].get_cluster_idx_by_venue(venue)
                    idx2 = self.co_org_venue_Cluster_list[j].get_cluster_idx_by_venue(venue)
                    cluster_co_org_venue_pair_set_list.append(set([idx1, idx2]))

        return cluster_co_org_venue_pair_set_list


